﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PHP
{
	public class ClassModle : Table
	{
		public List<bool> flagSet { get; set; }
		public List<bool> flagGet { get; set; }
		public bool flagConstruct { get; set; }
		public bool flagGetJson { get; set; }

		public bool flagModel { get; set; }
		public bool flagInsert { get; set; }
		public bool flagEdit { get; set; }
		public bool flagDelete { get; set; }
		public bool flagGetByID { get; set; }
		public bool flagGetAll { get; set; }
		public string KQ { get; set; }


		private string type(string str)
		{
			string st = string.Empty;
			string y = "$STATEMENT_TYPE_INTEGER";
			string d = "$STATEMENT_TYPE_DOUBLE";
			string s = "$STATEMENT_TYPE_STRING";
			string b = "$STATEMENT_TYPE_BLOB";
			if (str == "int(11)" || str =="tinyint(4)")
				return y;
			if (str =="double" || str=="float") 
				return d;
			if (str == "tinyint(1)")
				return b;
			return s;
		}
		public ClassModle()
		{
			flagSet = new List<bool>();
			flagGet = new List<bool>();

			flagConstruct = true; ;
			flagGetJson = true; ;

			flagModel = true; ;
			flagInsert = true; ;
			flagEdit = true; ;
			flagDelete = true; ;
			flagGetByID = true; ;
			flagGetAll = true; ;
			KQ = "";
					}
		public ClassModle(Table tmp):base(tmp)
		{

			flagSet = new List<bool>();
			flagGet = new List<bool>();

			for (int i = 0; i < tmp.nameColumn.Count; i++)
			{
				flagSet.Add(true);
				flagGet.Add(true);
			}
			flagConstruct = true; ;
			flagGetJson = true; ;

			flagModel = true; ;
			flagInsert = true; ;
			flagEdit = true; ;
			flagDelete = true; ;
			flagGetByID = true; ;
			flagGetAll = true;
			KQ = "";

		}
		private string vietHoa(string str)
		{
			//var ci = new CultureInfo("vi-VI");
			StringBuilder tmp = new StringBuilder("");
			char[] s = str.ToArray();
			if (s.Length > 0)
			{
				s[0] = char.ToUpper(s[0]);
			}
			foreach (var i in s)
			{
				tmp.Append(i);
			}
			return tmp.ToString();
		}
		private void hamCon(ref StringBuilder str,int n,string loaiHam)
		{
			// su dung cho ham insert/edit/delete

			//biet su dung dung tham so trong php thi rat tot
			//		$procName = "CALL sp_users(?,?,?,?)";
			str.Append("\n\t\t$procName = \"CALL sp_" + nameTable.Trim() + "(");
			for (int i = 0; i < n; i++)
			{
				if (i < n - 1)
				{
					str.Append("?,");//?,
				}
				else str.Append("?");//?
			}
			str.Append(")\";");
			//ten Proc(Mỗi dấu hỏi tương ứng với 1 tham số
			//			$names = array("p_id","p_userName","p_password","p_t");
			str.Append("\n\t\t$names = array(");
			for (int i = 0; i < n; i++)
			{
				//	p_id","p_userName","p_password","p_t");"
				if (i < n - 1)
				{
					str.Append(" \"p_" + nameColumn[i].ToString() + "\",");
				}
				else str.Append(" \"p_" + nameColumn[i].ToString() + "\"");
			}
			str.Append(");");

			//Mảng Chứa tên các tham số của Proc.
			//	$types = array(
			str.Append("\n\t\t$types = array(");
			for (int i = 0; i < n; i++)
			{
				if (i < n - 1)
				{
					str.Append("Statement_Parameter_Type ::" +type( typeOfCoumn[i].ToString() )+ ",");
				}
				else str.Append("Statement_Parameter_Type ::" + type(typeOfCoumn[i].ToString()));
				//	Statement_Parameter_Type ::$STATEMENT_TYPE_INTEGER,

			}
			//	);
			str.Append(");");
			//mảng chứa các kiểu dữ liệu tương ứng với giá trị
			//	$values = array(
			str.Append("\n\t\t$values = array(");
			for (int i = 0; i < n; i++)
			{
				str.Append("$" + nameTable.Trim() + "->Get" + nameColumn[i].ToString() + "(),");
				//	$user->GetId(),$user->GetUserName(),$user->GetPassWord(),
			}
			//	0);// 0 insert
			str.Append(loaiHam+" );");

			//	DataProvider::ExecuteQueryParamter($procName, $names, $values, $types);
			str.Append("\n\t\tDataProvider::ExecuteQueryParamter($procName, $names, $values, $types);");
		}
		public string taoClass()
		{
			string tmp = "";

			int n = nameColumn.Count;
			StringBuilder str = new StringBuilder();
			str.Append("<?PHP");

			str.Append("\nclass " + vietHoa(this.nameTable.Trim()) + "{");
			str.Append("\n");
			for (int i = 0; i < n; i++)
			{
				//các biến
				str.Append("\t private $" + nameColumn[i].ToString().Trim() + ";");
				str.Append("\n");
			}
			str.Append("\n");
			//construct
			if (flagConstruct )
			{
				str.Append("\tpublic function __construct(");
				for (int i = 0; i < n - 1; i++)
				{
					str.Append("$" + nameColumn[i].ToString() + "=\"\",");
				}
				str.Append("$" + nameColumn[n-1].ToString() + "=\"\"){");
				for (int i = 0; i < n; i++)
				{
					str.Append("\n\t\t$this->" + nameColumn[i].ToString().Trim() + "=" + "$" + nameColumn[i].ToString().Trim() + ";");
				}
				str.Append("\n\t}");
			}
			str.Append("\n");

			// JSon
			if (flagGetJson)
			{
				str.Append("\tpublic function getJson(){");
				str.Append("\n\t\t$item = array(");
				for (int i = 0; i < n - 1; i++)
				{
					str.Append("\n\t\t\t'" + nameColumn[i].ToString().Trim() + "'=>$this->" + nameColumn[i].ToString().Trim() + ",");
				}
				str.Append("\n\t\t\t'" + nameColumn[n-1].ToString().Trim() + "'=>$this->" + nameColumn[n-1].ToString().Trim());
				str.Append("\n\t\t);");
				str.Append("\n\t\treturn $item;");
				str.Append("\n\t}");
			}
			str.Append("\n");
			// hàm set
			for (int i = 0; i < n; i++)
			{
					if (flagSet[i])
					{
						str.Append("\tpublic function Set" + vietHoa(nameColumn[i].ToString().Trim()) + "($" + nameColumn[i].ToString().Trim() + "){");
						str.Append("\n\t\t $this->" + nameColumn[i].ToString().Trim() + " = $" + nameColumn[i].ToString().Trim() + ";");
						str.Append("\n\t}\n");
					}
			}

			str.Append("\n");

			// hàm get
			for (int i = 0; i < n; i++)
			{
					if (flagGet[i])
					{
						str.Append("\n\tpublic function Get" + vietHoa(nameColumn[i].ToString().Trim()) + "(){");
						str.Append("\n\t\t return $this->" + nameColumn[i].ToString().Trim() + ";");
						str.Append("\n\t}");
					}
			}
			//cuối class
			str.Append("\n}");

			//MODLE


			str.Append("\nclass " + vietHoa(nameTable.Trim()) + "Model{");
			if (flagInsert)
			{
				str.Append("\n\tpublic function Insert($" + vietHoa(nameTable.Trim()) + "){");

				hamCon(ref str, n, "0");

				str.Append("\n\t}");

			}
			if (flagEdit)
			{
				str.Append("\n\tpublic function Edit($" + vietHoa(nameTable.Trim()) + "){");
				hamCon(ref str, n, "1");
				str.Append("\n\t}");
			}
			if (flagDelete)
			{
				str.Append("\n\tpublic function Delete($" + vietHoa(nameTable.Trim()) + "){");
				hamCon(ref str, n, "2");
				str.Append("\n\t}");

			}

			// Hien thi thong tin 1 ob
			if (flagGetByID)
			{
				str.Append("\n\tpublic function Get" + vietHoa(nameTable) + "ById($" + nameColumn[0].Trim() + "){");
				str.Append("\n\t\t$mysql = \"select * from " + vietHoa(nameTable) + " where " + nameColumn[0].Trim() + " = \" . $" + nameColumn[0].Trim() + ". \";\";");
				str.Append("\n\t\t$results = DataProvider::ExecuteQuery($mysql);");
				str.Append("\n\t\t$data = array();");
				str.Append("\n\t\tif($results != FALSE){");
				str.Append("\n\t\t\twhile ($row = mysql_fetch_array($results,MYSQL_ASSOC)){");
				str.Append("\n\t\t\t\t$item = new " + vietHoa(nameTable.Trim()) + " (");
				//cac bien trong class
				for (int i = 0; i < n; i++)
				{
					if (i != n - 1) str.Append(" $row[" + nameColumn[i].ToString().Trim() + "],");
					else str.Append(" $row[" + nameColumn[i].ToString().Trim() + "]");
				}
				str.Append(");");
				str.Append("\n\t\t\t\t$data[] = $item->getJson();");
				str.Append("\n\t\t\t}");
				str.Append("\n\t\t}");
				str.Append("\n\t\treturn $data;");
				str.Append("\n\t}");
			}
			//lay tat ca danh sach users
			str.Append("\n\tpublic function GetAll" + vietHoa(nameTable.Trim()) + "(){");
			str.Append("\n\t\t$mysql = \"select * from " + vietHoa(nameTable.Trim()) + ";\";");
			str.Append("\n\t\t$results = DataProvider::ExecuteQuery($mysql);");
			str.Append("\n\t\t$data = array();");
			str.Append("\n\t\tif($results != FALSE){");
			str.Append("\n\t\t\twhile ($row = mysql_fetch_array($results,MYSQL_ASSOC)){");
			str.Append("\n\t\t\t\t$item = new " + vietHoa(nameTable.Trim()) + " (");
			//cac bien trong class
			for (int i = 0; i < n; i++)
			{
				if (i != n - 1) str.Append(" $row[" + nameColumn[i].ToString().Trim() + "],");
				else str.Append(" $row[" + nameColumn[i].ToString().Trim() + "]");
			}
			str.Append(");");
			str.Append("\n\t\t\t\t$data[] = $item->getJson();");
			str.Append("\n\t\t\t}");
			str.Append("\n\t\t}");
			str.Append("\n\t\treturn $data;");
			str.Append("\n\t}");
			str.Append("\n}");
			//END MODLE
			str.Append("\n?>");
			KQ = str.ToString();
			return KQ;
		}
	}
}
/* chuoi insert
			str.Append("\n\tpublic function Insert($" + vietHoa(nameTable.Trim()) + "){");
			str.Append("\n\t\t\t$mysql = \"INSERT INTO 'bdwal'.'" + vietHoa(nameTable) + "' (");
			//ten cac cot
			for (int i = 0; i < n - 1; i++)
			{
				str.Append("'" + vietHoa(nameColumn[i].ToString()) + "',");
			}
			str.Append("\'" + vietHoa(nameColumn[n-1].ToString()) + "\'");

			str.Append(")\";");
			//value
			str.Append("\n\t\t\t$mysql.=\"VALUES(\";");
			for (int i = 0; i < n - 1; i++)
			{
				str.Append("\n\t\t\t$mysql.=$" + vietHoa(nameTable) + "->" + nameColumn[i].ToString() + ".\",\";");
			}
			str.Append("\n\t\t\t$mysql.=$" + vietHoa(nameTable) + "->" + nameColumn[n-1].ToString());

			str.Append(".\");\";");

			str.Append("\n\t\t\tDataProvider :: ExecuteQuery($mysql);");

			str.Append("\n\t}");
			 */